// pages/member/myEvaluation/myEvaluation.js
var app = getApp();
Page({
  /**
	 * 页面的初始数据
	 */
  data: {
    language: '',
    selected: 0,
    //待评价
    wait: [],
    commentLength: '',
    waitLength:'',
    isLoadMore: false,
    waitPage: 1,
    waitLastPage: 0,
    //已评价
    //其他
    other: []
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {

  },

  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {
    let language = wx.getStorageSync('langIndex');
    this.setData({ 'language': language.en});
  },

  /**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function() {
    this._initData();
    this._getNetData(this.data.selected);
  },

  /**
	 * 生命周期函数--监听页面隐藏
	 */
  onHide: function() {

  },

  /**
	 * 生命周期函数--监听页面卸载
	 */
  onUnload: function() {

  },

  /**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function() {

  },

  /**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function() {
    this.getMoreData();
  },

  /**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function() {

  },
  _getNetData(status) {
    let urlStr = app.getNetAddresss("order.my-comment.paging");
    if(app.globalData.store_alone_temp == 1) {
      // 开启门店独立模块
      urlStr = app.getNetAddresss("plugin.store-alone-temp.frontend.member.my-comment");
    }
    urlStr += `&page=${this.data.waitPage}&status=${status}`;
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        let response = resdata.data;
        let data = response.data;
        let list = data.list.data;
        let last_page = data.list.last_page;
        if (response.result == 1) {
          Number(status)===1?this.data.commentLength = list.length:this.data.waitLength = list.length;
          this.setData({
            commentLength:this.data.commentLength,
            waitLength: this.data.waitLength,
            wait: list??[],
            waitLoadMore: true,
            waitLastPage: last_page??0,
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },

  // 加载更多
  //获取更多数据
  getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.waitPage >= this.data.waitLastPage) {
      return;
    } else {
      this.data.waitPage = this.data.waitPage + 1;
      let urlStr = app.getNetAddresss("order.my-comment.paging");
      if(app.globalData.store_alone_temp == 1) {
        // 开启门店独立模块
        urlStr = app.getNetAddresss("plugin.store-alone-temp.frontend.member.my-comment");
      }
      urlStr += `&page=${this.data.waitPage}&status=${this.data.selected}`;
      app._getNetWork({
        url: urlStr,
        data: {},
        success: (resdata) => {
          let res = resdata.data;
          let data = res.data;
          let list =data.list.data;
          if (res.result == 1) {
            this.data.isLoadMore = true;
            let waitList = this.data.wait.concat(list);
            Number(this.data.selected)===1?this.data.commentLength = waitList?.length || 0:this.data.waitLength = waitList?.length || 0;
            this.setData({
              wait :waitList,
              commentLength:this.data.commentLength,
              waitLength: this.data.waitLength,
            })
          } else {
            this.data.waitPage = this.data.waitPage - 1;
            this.isLoadMore = false;
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  

  _initData() {
    this.setData({
      selected: 0,
      //待评价
      wait: [],
      waitLoadMore: false,
      waitPage: 1,
      waitLastPage: 0,
      //已评价
      comment: [],
      commentLoadMore: false,
      commentPage: 1,
      commentLastPage: 0,
      commentLength: '',
      waitLength:'',
      //其他
      other: []
    });
  },
  swichTabTItem(e) {
    let index = e.detail.index;
    this.setData({
      selected: index,
      wait: [],
      waitLoadMore: false,
      waitLastPage: 0,
      waitPage: 1
    });
    this._getNetData(this.data.selected);
  },
});
